package cae.windows
{
	import mx.containers.Canvas;
	import flash.display.DisplayObject;
	import mx.core.Container;
	import mx.core.ClassFactory;
	import flash.utils.getDefinitionByName;
	import mx.controls.Alert;
	import mx.effects.WipeDown;
	import caurina.transitions.Equations;
	import flash.events.Event;

	public class CaeWindowsManager extends Canvas
	{
		/* EFFECTO DE  APERTURA DE VENTANA */
		private var vpd:WipeDown;
		
		/* VENTANA INICIAL */
		public var initOption:String;

		public function CaeWindowsManager()
		{
			super();
			vpd = new WipeDown();
			vpd.duration = 500;
			vpd.moveEasingFunction = Equations.easeOutBounce;
			addEventListener(Event.ADDED_TO_STAGE, initWindowsManager);
		}
		
		private function initWindowsManager(e:Event):void
		{
			if(initOption != null)
			{
				createWindow(null, initOption, null);
			}
		}
		
		private function addWindow(displayName:String, c:CaeWindow, dataId:Object):void
		{
			removeAllChildren();
			c.initOption(dataId);
			c.setStyle("addedEffect", vpd);
			c.percentHeight = 100;
			c.percentWidth = 100;
			
			addChild(DisplayObject(c));
		}
		
		public function createWindow(displayName:String, className:String, dataId:Object):void
		{
			var myInstance:Object;
			
			try
			{
				var definition:Class = getDefinitionByName(className) as Class;
				myInstance = new definition(); 
			}
			catch(e:ReferenceError)
			{
				Alert.show("No se ha encontrado la ventana: " + className, "ERROR");
				return;
			}
			
			if( !(myInstance is CaeWindow) )
			{
				Alert.show("El tipo de ventana no corresponde, cancelando apertura", "Error");
				return;
			}
			addWindow(displayName, CaeWindow(myInstance), dataId);
		}
	}
}